﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using fideeraSoft.Utils;
using System.Data.SqlClient;
using fideeraSoft.Handlers;

namespace fideeraSoft.Paneles
{
    class AdministracionPanel :PanelGeneral
    {
        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.Button btTestDb;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.TextBox tbCustomerQuery;
        private System.Windows.Forms.Button btCustomQuery;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox LoginDB;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox PassDB;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button RunQueryCreateLoginDB;
        private System.Windows.Forms.TextBox UserDB;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.GroupBox groupBox3;
        private System.Windows.Forms.Button btListarLogins;
        private System.Windows.Forms.DataGridView dgvListarLogins;


        public AdministracionPanel(Handler handler)
        {
            this.formatearPanelGeneral(handler);

            this.InitializeComponent();            

            groupBox1.Controls.Add(btTestDb);

            groupBox3.Controls.Add(UserDB);
            groupBox3.Controls.Add(PassDB);
            groupBox3.Controls.Add(LoginDB);

            groupBox2.Controls.Add(dgvListarLogins);


            this.Controls.Add(groupBox1);
            this.Controls.Add(groupBox3);
            this.Controls.Add(groupBox2);
            

        }


        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.label4 = new System.Windows.Forms.Label();
            this.LoginDB = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.PassDB = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.RunQueryCreateLoginDB = new System.Windows.Forms.Button();
            this.UserDB = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.btCustomQuery = new System.Windows.Forms.Button();
            this.tbCustomerQuery = new System.Windows.Forms.TextBox();
            this.btTestDb = new System.Windows.Forms.Button();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.btListarLogins = new System.Windows.Forms.Button();
            this.dgvListarLogins = new System.Windows.Forms.DataGridView();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.panel1.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.tabPage2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dgvListarLogins)).BeginInit();
            this.SuspendLayout();
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this.tabControl1.Location = new System.Drawing.Point(29, 66);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(922, 482);
            this.tabControl1.TabIndex = 1;
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.groupBox2);
            this.tabPage1.Controls.Add(this.groupBox1);
            this.tabPage1.Controls.Add(this.btTestDb);
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage1.Size = new System.Drawing.Size(914, 456);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "AdminDB";
            this.tabPage1.UseVisualStyleBackColor = true;
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.panel1);
            this.groupBox2.Location = new System.Drawing.Point(91, 44);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(265, 224);
            this.groupBox2.TabIndex = 6;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Login User Pass DB";
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.label4);
            this.panel1.Controls.Add(this.LoginDB);
            this.panel1.Controls.Add(this.label3);
            this.panel1.Controls.Add(this.PassDB);
            this.panel1.Controls.Add(this.label2);
            this.panel1.Controls.Add(this.RunQueryCreateLoginDB);
            this.panel1.Controls.Add(this.UserDB);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Location = new System.Drawing.Point(22, 17);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(225, 200);
            this.panel1.TabIndex = 5;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label4.Location = new System.Drawing.Point(27, 47);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(48, 20);
            this.label4.TabIndex = 10;
            this.label4.Text = "Login";
            // 
            // LoginDB
            // 
            this.LoginDB.Location = new System.Drawing.Point(87, 49);
            this.LoginDB.Name = "LoginDB";
            this.LoginDB.Size = new System.Drawing.Size(100, 20);
            this.LoginDB.TabIndex = 1;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(22, 14);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(154, 13);
            this.label3.TabIndex = 8;
            this.label3.Text = "Crear nuevo login y user en DB";
            // 
            // PassDB
            // 
            this.PassDB.Location = new System.Drawing.Point(87, 102);
            this.PassDB.Name = "PassDB";
            this.PassDB.Size = new System.Drawing.Size(100, 20);
            this.PassDB.TabIndex = 3;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.Location = new System.Drawing.Point(31, 100);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(44, 20);
            this.label2.TabIndex = 6;
            this.label2.Text = "Pass";
            // 
            // RunQueryCreateLoginDB
            // 
            this.RunQueryCreateLoginDB.Location = new System.Drawing.Point(87, 149);
            this.RunQueryCreateLoginDB.Name = "RunQueryCreateLoginDB";
            this.RunQueryCreateLoginDB.Size = new System.Drawing.Size(75, 23);
            this.RunQueryCreateLoginDB.TabIndex = 4;
            this.RunQueryCreateLoginDB.Text = "Run";
            this.RunQueryCreateLoginDB.UseVisualStyleBackColor = true;
            this.RunQueryCreateLoginDB.Click += new System.EventHandler(this.RunQueryCreateLoginDB_Click);
            // 
            // UserDB
            // 
            this.UserDB.Location = new System.Drawing.Point(87, 75);
            this.UserDB.Name = "UserDB";
            this.UserDB.Size = new System.Drawing.Size(100, 20);
            this.UserDB.TabIndex = 2;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(31, 73);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(43, 20);
            this.label1.TabIndex = 3;
            this.label1.Text = "User";
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.btCustomQuery);
            this.groupBox1.Controls.Add(this.tbCustomerQuery);
            this.groupBox1.Location = new System.Drawing.Point(365, 44);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(496, 224);
            this.groupBox1.TabIndex = 5;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Customer Query";
            // 
            // btCustomQuery
            // 
            this.btCustomQuery.Location = new System.Drawing.Point(15, 149);
            this.btCustomQuery.Name = "btCustomQuery";
            this.btCustomQuery.Size = new System.Drawing.Size(75, 23);
            this.btCustomQuery.TabIndex = 1;
            this.btCustomQuery.Text = "Run CQ";
            this.btCustomQuery.UseVisualStyleBackColor = true;
            this.btCustomQuery.Click += new System.EventHandler(this.btCustomQuery_Click_1);
            // 
            // tbCustomerQuery
            // 
            this.tbCustomerQuery.Location = new System.Drawing.Point(15, 34);
            this.tbCustomerQuery.Multiline = true;
            this.tbCustomerQuery.Name = "tbCustomerQuery";
            this.tbCustomerQuery.Size = new System.Drawing.Size(464, 108);
            this.tbCustomerQuery.TabIndex = 0;
            // 
            // btTestDb
            // 
            this.btTestDb.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
            this.btTestDb.ForeColor = System.Drawing.SystemColors.ActiveCaption;
            this.btTestDb.Location = new System.Drawing.Point(6, 44);
            this.btTestDb.Name = "btTestDb";
            this.btTestDb.Size = new System.Drawing.Size(79, 41);
            this.btTestDb.TabIndex = 2;
            this.btTestDb.Text = "CONNECT TO SQL";
            this.btTestDb.UseVisualStyleBackColor = false;
            this.btTestDb.Click += new System.EventHandler(this.btTestDb_Click);
            // 
            // tabPage2
            // 
            this.tabPage2.Controls.Add(this.groupBox3);
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage2.Size = new System.Drawing.Size(914, 456);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Users & Logins DB";
            this.tabPage2.UseVisualStyleBackColor = true;
            // 
            // groupBox3
            // 
            this.groupBox3.Controls.Add(this.btListarLogins);
            this.groupBox3.Controls.Add(this.dgvListarLogins);
            this.groupBox3.Location = new System.Drawing.Point(39, 36);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(438, 404);
            this.groupBox3.TabIndex = 2;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Listar Logins DB";
            // 
            // btListarLogins
            // 
            this.btListarLogins.ForeColor = System.Drawing.Color.Crimson;
            this.btListarLogins.Location = new System.Drawing.Point(180, 375);
            this.btListarLogins.Name = "btListarLogins";
            this.btListarLogins.Size = new System.Drawing.Size(75, 23);
            this.btListarLogins.TabIndex = 4;
            this.btListarLogins.Text = "Run Listar";
            this.btListarLogins.UseVisualStyleBackColor = true;
            this.btListarLogins.Click += new System.EventHandler(this.btListarLogins_Click_1);
            // 
            // dgvListarLogins
            // 
            this.dgvListarLogins.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvListarLogins.Location = new System.Drawing.Point(15, 25);
            this.dgvListarLogins.Name = "dgvListarLogins";
            this.dgvListarLogins.Size = new System.Drawing.Size(400, 327);
            this.dgvListarLogins.TabIndex = 3;
            this.dgvListarLogins.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvListarLogins_CellContentClick);
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.tabPage2.ResumeLayout(false);
            this.groupBox3.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dgvListarLogins)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private void RunQueryCreateLoginDB_Click(object sender, EventArgs e)
        {
            SQLclass sqlcl = new SQLclass();

            if ((LoginDB.Text != "") && (UserDB.Text != "") && (PassDB.Text != ""))
            {
                try
                {
                    sqlcl.createUserDB(LoginDB.Text, UserDB.Text, PassDB.Text);
                    MessageBox.Show("USER LOGIN CREADO CORRECTAMENTE", "USER CREATED", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (MiException ex)
                {
                    MessageBox.Show("ERROR AL CREAR NUEVO LOGIN USUARIO DB\n" + ex, "EXCEPTION", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                finally
                {

                }

            }
            else
            {
                MessageBox.Show("LOS CAMPOS ESTAN VACIOS", "FALLO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

               

        private void dvgListarLogins_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            //place your own dialog or do nonthing here
        }

        private void dgvListarLogins_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btCustomQuery_Click_1(object sender, EventArgs e)
        {

            if (tbCustomerQuery.Text != "")
            {
                MessageBox.Show("LOS CAMPOS ESTAN VACIOS", "FALLO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                MessageBox.Show("LOS CAMPOS ESTAN VACIOS", "FALLO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btListarLogins_Click_1(object sender, EventArgs e)
        {

            SQLclass sqlc = new SQLclass();
            //dgvListarLogins = new DataGridView();

            dgvListarLogins.DataSource = null;


            try
            {

                dgvListarLogins.DataSource = sqlc.listarLogins();

            }
            catch (MiException ex)
            {
                MessageBox.Show("Error al listar logins.\n" + ex, "EXCEPTION", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
        }

        private void btTestDb_Click(object sender, EventArgs e)
        {
            
            try
            {

                // Call the MessageBox function using platform invoke.
                SQLclass sqlClass = new SQLclass();

                sqlClass.ConnectToSql();
               

                MessageBox.Show("Hello World!", "Hello Dialog", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                // MessageBox msg = new MessageBox();
            }
            catch(Exception ex)
            {
                CLogger.Append("ERROR AL CONECTAR SQL CONNECT EXCEPTION: " + ex, CLogger.ERROR);      //DEBUG
            }
            finally
            {
               
            }
        }
    }
}
